﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WGM
{
    class ClassSQL
    {
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=" + AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\gamesdb.mdb");

        string sqlresult1 = "";
        string sqlresult2 = "";
        string sqlresult3 = "";
        string sqlresult4 = "";
        string sqlresult5 = "";
        string sqlresult6 = "";
        string sqlresult7 = "";
        string sqlresult8 = "";
        string sqlresult9 = "";
        string sqlresult10 = "";
        string sqlresult11 = "";
        string sqlresult12 = "";
        string sqlresult13 = "";
        string sqlresult14 = "";
        string sqlresult15 = "";
        string sqlresult16 = "";
        string sqlresult17 = "";
        string sqlresult18 = "";

        string sqlresultt1 = "";
        string sqlresultt2 = "";
        string sqlresultt3 = "";
        string sqlresultt4 = "";
        string sqlresultt5 = "";

        public void gamesfill(PageGames Window, string sql)
        {
            Window.listbox_gamelist.Items.Clear();

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listbox_gamelist.SelectedIndex = 0;
        }

        public void gamesfill_special(PageGames Window, string sql, string requirement)
        {
            Window.listbox_gamelist.Items.Clear();

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            DateTime m70 = DateTime.Now;
            string m700 = DateTime.Now.ToString("D");
            DateTime m71 = m70.AddDays(-1);
            string m711 = m71.ToString("D");
            DateTime m72 = m70.AddDays(-2);
            string m722 = m72.ToString("D");
            DateTime m73 = m70.AddDays(-3);
            string m733 = m73.ToString("D");
            DateTime m74 = m70.AddDays(-4);
            string m744 = m74.ToString("D");
            DateTime m75 = m70.AddDays(-5);
            string m755 = m75.ToString("D");
            DateTime m76 = m70.AddDays(-6);
            string m766 = m76.ToString("D");
            DateTime m77 = m70.AddDays(-7);
            string m777 = m77.ToString("D");

            try
            {
                while (reader.Read())
                {
                    sqlresult12 = reader.GetString(12).ToString();
                    sqlresult13 = reader.GetString(13).ToString();
                    sqlresult14 = reader.GetString(14).ToString();
                    sqlresult15 = reader.GetString(15).ToString();

                    CultureInfo ciCurr = CultureInfo.CurrentCulture;
                    int weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
                    string playweek = weekNum.ToString();

                    switch (requirement)
                    {
                        case "IP":
                            if (Properties.Settings.Default.ipstatus == "3")
                            {
                                if (sqlresult12 == m700 || sqlresult12 == m711 || sqlresult12 == m722 || sqlresult12 == m733)
                                {
                                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                }
                            }

                            if (Properties.Settings.Default.ipstatus == "7")
                            {
                                if (sqlresult12 == m700 || sqlresult12 == m711 || sqlresult12 == m722 || sqlresult12 == m733 || sqlresult12 == m744 || sqlresult12 == m755 || sqlresult12 == m766 || sqlresult12 == m777)
                                {
                                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                }
                            }

                            if (Properties.Settings.Default.ipstatus == "8")
                            {
                                if (sqlresult14 == playweek)
                                {
                                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                }
                            }

                            if (Properties.Settings.Default.ipstatus == "31")
                            {
                                if (sqlresult15 == DateTime.Today.Month.ToString())
                                {
                                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                }
                            }
                            break;

                        case "UF":
                            if (Properties.Settings.Default.ipstatus == "3")
                            {
                                if (sqlresult12 != m700 && sqlresult12 != m711 && sqlresult12 != m722 && sqlresult12 != m733 && sqlresult13 != "Yes")
                                {
                                    if (sqlresult12 != "Never Played")
                                    {
                                        Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                    }
                                }
                            }

                            if (Properties.Settings.Default.ipstatus == "7")
                            {
                                if (sqlresult12 != m700 && sqlresult12 != m711 && sqlresult12 != m722 && sqlresult12 != m733 && sqlresult13 != "Yes" && sqlresult12 != m744 && sqlresult12 != m755 && sqlresult12 != m766 && sqlresult12 != m777)
                                {
                                    if (sqlresult12 != "Never Played")
                                    {
                                        Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                    }
                                }
                            }

                            if (Properties.Settings.Default.ipstatus == "8")
                            {
                                if (sqlresult14 != playweek && sqlresult14 != "0" && sqlresult13 != "Yes")
                                {
                                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                }
                            }

                            if (Properties.Settings.Default.ipstatus == "31")
                            {
                                if (sqlresult15 != DateTime.Today.Month.ToString() && sqlresult15 != "0" && sqlresult13 != "Yes")
                                {
                                    Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                                }
                            }
                            break;

                        case "US":
                            if (sqlresult12 == "Never Played" && sqlresult13 != "Yes")
                            {
                                Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                            }
                            break;

                        case "CO":
                            if (sqlresult13 == "Yes")
                            {
                                Window.listbox_gamelist.Items.Add(reader.GetString(1).ToString());
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listbox_gamelist.SelectedIndex = 0;
        }

        public void gamesresult(string sql)
        {
            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    //Game Name
                    sqlresult1 = reader.GetString(1).ToString();
                    //Save Game
                    sqlresult2 = reader.GetString(2).ToString();
                    //Compatibility
                    sqlresult3 = reader.GetString(3).ToString();
                    //Backup State
                    sqlresult4 = reader.GetString(4).ToString();
                    //Status
                    sqlresult5 = reader.GetString(5).ToString();
                    //Type
                    sqlresult6 = reader.GetString(6).ToString();
                    //Note
                    sqlresult7 = reader.GetString(7).ToString();
                    //Plus
                    sqlresult8 = reader.GetString(8).ToString();
                    //Moin
                    sqlresult9 = reader.GetString(9).ToString();
                    //Description
                    sqlresult10 = reader.GetString(10).ToString();
                    //Path
                    sqlresult11 = reader.GetString(11).ToString();
                    //Last Played
                    sqlresult12 = reader.GetString(12).ToString();
                    //Completed
                    sqlresult13 = reader.GetString(13).ToString();
                    //CompletedResult
                    sqlresult16 = reader.GetString(16).ToString();
                    //CompletedStyle
                    sqlresult17 = reader.GetString(17).ToString();
                    //Favorite
                    sqlresult18 = reader.GetString(18).ToString();
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }

            finally
            {
                reader.Close(); conn.Close();
            }
        }

        public void datarefresh(PageGames Window)
        {
            if (Window.listbox_gamelist.SelectedIndex != -1)
            {
                string gamename = Window.listbox_gamelist.SelectedItem.ToString();
                string sql = "SELECT * FROM gamecentral WHERE gamename = '" + gamename + "'";

                if (gamename == "Defiance")
                {
                    Window.tabitem_defiance.Visibility = Visibility.Visible;
                }
                else
                {
                    Window.tabitem_defiance.Visibility = Visibility.Collapsed;
                }

                gamesresult(sql);

                Window.textbox_rtype.Text = sqlresult6;
                Window.combobox_type.Text = sqlresult6;

                if (sqlresult18 == "Yes")
                {
                    Window.switch_favorite.IsChecked = true;
                    Window.menuitem_selected5.IsEnabled = false;
                }
                else
                {
                    Window.switch_favorite.IsChecked = false;
                    Window.menuitem_selected5.IsEnabled = true;
                }

                if (sqlresult13 == "Yes")
                {
                    Window.checkbox_completed.IsChecked = true;
                }
                else
                {
                    Window.checkbox_completed.IsChecked = false;
                }

                DateTime m70 = DateTime.Now;
                string m700 = DateTime.Now.ToString("D");
                DateTime m71 = m70.AddDays(-1);
                string m711 = m71.ToString("D");
                DateTime m72 = m70.AddDays(-2);
                string m722 = m72.ToString("D");
                DateTime m73 = m70.AddDays(-3);
                string m733 = m73.ToString("D");
                DateTime m74 = m70.AddDays(-4);
                string m744 = m74.ToString("D");
                DateTime m75 = m70.AddDays(-5);
                string m755 = m75.ToString("D");
                DateTime m76 = m70.AddDays(-6);
                string m766 = m76.ToString("D");
                DateTime m77 = m70.AddDays(-7);
                string m777 = m77.ToString("D");

                if (sqlresult12 == DateTime.Now.ToString("D") || sqlresult12 == m711 || sqlresult12 == m722 || sqlresult12 == m733 || sqlresult12 == m744 || sqlresult12 == m755 || sqlresult12 == m766 || sqlresult12 == m777)
                {
                    Window.textbox_rstatus.Text = "In Progress";
                }
                else
                {
                    Window.textbox_rstatus.Text = "Unfinished";
                }

                if (sqlresult12 == "Never Played")
                {
                    Window.textbox_rstatus.Text = "Unstarted";
                }

                if (sqlresult13 == "Yes")
                {
                    Window.textbox_rstatus.Text = "Completed";

                    if (sqlresult16 != "Prior to WGM")
                    {
                        completiongstatus(gamename);
                        Window.textbox_rcompleted.Text = sqlresult16 + " Times / " + sqlresultt3;
                    }
                    else
                    {
                        Window.textbox_rcompleted.Text = sqlresult16;
                    }
                }
                else
                {
                    Window.textbox_rcompleted.Text = "Never Completed";
                }

                if (sqlresult2 == "NOT SET")
                {
                    Window.textbox_rbackup.Text = "No";
                    Window.button_path.Content = "Set path to Savegame";
                    Window.menuitem_selected1.IsEnabled = false;
                }
                else
                {
                    Window.textbox_rbackup.Text = "Yes";
                    Window.button_path.Content = "Change path to Savegame";
                    Window.menuitem_selected1.IsEnabled = true;
                }

                if (sqlresult11 == "NOT SET")
                {
                    Window.button_exe.Content = "Set path to Exe";
                    Window.menuitem_selected0.IsEnabled = false;
                }
                else
                {
                    Window.button_exe.Content = "Change path to Exe";
                    Window.menuitem_selected0.IsEnabled = true;
                }

                Window.textbox_rcomp.Text = sqlresult3;
                Window.combobox_comp.Text = sqlresult3;
                Window.textbox_descp.Text = sqlresult10;
                Window.textbox_descpedit.Text = sqlresult10;
                Window.textbox_rlastplayed.Text = sqlresult12;

                if (sqlresult11 == "NOT SET")
                {
                    Window.textbox_rplaytype.Text = "Unknow";
                    Window.label_gamestart.Visibility = Visibility.Hidden;
                    Window.label_gamestart_text.Visibility = Visibility.Hidden;
                }
                else
                {
                    Window.textbox_rplaytype.Text = "HDD";
                    Window.label_gamestart.Visibility = Visibility.Visible;
                    Window.label_gamestart_text.Visibility = Visibility.Visible;
                }

                if (sqlresult11.Contains("steam"))
                {
                    Window.textbox_rplaytype.Text = "Steam";
                    Window.label_gamestart.Visibility = Visibility.Visible;
                    Window.label_gamestart_text.Visibility = Visibility.Visible;
                }

                if (sqlresult11.Contains("Dolphin"))
                {
                    Window.textbox_rplaytype.Text = "Wii";
                    Window.label_gamestart.Visibility = Visibility.Visible;
                    Window.label_gamestart_text.Visibility = Visibility.Visible;
                }

                gamestime(gamename);

                Window.textbox_rplaytime.Text = sqlresultt1 + " / " + sqlresultt2;

                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamename + ".jpg"))
                {
                    Window.image_cover.Stretch = Stretch.Fill;
                    Window.image_cover.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + gamename + ".jpg"));
                    Window.button_cover.IsEnabled = false;
                }
                else
                {
                    Window.image_cover.Stretch = Stretch.Uniform;
                    Window.image_cover.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Resources\WGM_icon_dark.png"));
                    Window.button_cover.IsEnabled = true;
                }

                Window.comboxbox_note.Text = sqlresult7;
                Window.textbox_note.Text = sqlresult7;
                Window.textbox_thegood.Text = sqlresult8;
                Window.textbox_thebad.Text = sqlresult9;
            }
        }

        private void completiongstatus(string gamename)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT gamelink, completedstatus, completedtype, playday, playmonth, playyear FROM timetrack WHERE completedstatus Is Not Null AND gamelink = '" + gamename + "'";


                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }
            string t1 = dtResults.Rows[0]["completedtype"].ToString();
            string t2 = dtResults.Rows[0]["playyear"].ToString();
            string t3 = dtResults.Rows[0]["playmonth"].ToString();
            string t4 = dtResults.Rows[0]["playday"].ToString();

            switch (t3)
            {
                case "1":
                    t3 = "January";
                    break;
                case "2":
                    t3 = "February";
                    break;
                case "3":
                    t3 = "March";
                    break;
                case "4":
                    t3 = "April";
                    break;
                case "5":
                    t3 = "May";
                    break;
                case "6":
                    t3 = "June";
                    break;
                case "7":
                    t3 = "July";
                    break;
                case "8":
                    t3 = "August";
                    break;
                case "9":
                    t3 = "September";
                    break;
                case "10":
                    t3 = "October";
                    break;
                case "11":
                    t3 = "November";
                    break;
                case "12":
                    t3 = "December";
                    break;
            }

            sqlresultt3 = "Last completed in " + t3 + " " + t4 + ", " + t2 + " on " + t1;
        }

        private void gamestime(string gamename)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT SUM([sessiontime]) AS TOTAL FROM timetrack WHERE gamelink = '" + gamename + "'";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            Int32 timeresult;
            Int32.TryParse(dtResults.Rows[0]["TOTAL"].ToString(), out timeresult);

            sqlresultt1 = timeresult + " Minutes";
            sqlresultt2 = timeresult / 60 + " Hours";
        }

        public void gamestart(string sql)
        {
            gamesresult(sql);

            try
            {
                var processStartInfo = new ProcessStartInfo(sqlresult11);
                processStartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(sqlresult11);
                Process gameProcess = Process.Start(processStartInfo);
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
        }

        public void gamesupdate(string sql)
        {
            conn.Open();
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public void total_gamestime(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT SUM([sessiontime]) AS TOTAL FROM timetrack";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            Int32 timeresult;
            Int32.TryParse(dtResults.Rows[0]["TOTAL"].ToString(), out timeresult);

            Window.textbox_playtime.Text = timeresult + " Minutes / " + timeresult / 60 + " Hours";
        }

        public void total_gamestimeday(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            int playday = DateTime.Today.Day;

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT SUM([sessiontime]) AS TOTAL FROM timetrack WHERE playday=" + playday + "";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            Int32 timeresult;
            Int32.TryParse(dtResults.Rows[0]["TOTAL"].ToString(), out timeresult);

            Window.textbox_resumtoday.Text = timeresult + " Minutes / " + timeresult / 60 + " Hours";
        }

        public void total_gamestimeweek(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            int playweek = weekNum;

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT SUM([sessiontime]) AS TOTAL FROM timetrack WHERE playweek=" + playweek + "";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            Int32 timeresult;
            Int32.TryParse(dtResults.Rows[0]["TOTAL"].ToString(), out timeresult);

            Window.textbox_resumweek.Text = timeresult + " Minutes / " + timeresult / 60 + " Hours";
        }

        public void total_gamestimemonth(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            int playmonth = DateTime.Today.Month;

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT SUM([sessiontime]) AS TOTAL FROM timetrack WHERE playmonth=" + playmonth + "";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            Int32 timeresult;
            Int32.TryParse(dtResults.Rows[0]["TOTAL"].ToString(), out timeresult);

            Window.textbox_resummonth.Text = timeresult + " Minutes / " + timeresult / 60 + " Hours";
        }

        public void avg_gamestime(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT AVG([sessiontime]) AS AVERAGE FROM timetrack";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }
            string rawVal = dtResults.Rows[0]["AVERAGE"].ToString();
            Double numValM = Math.Round(Convert.ToDouble(rawVal), 1, MidpointRounding.ToEven);
            Double numValH = Math.Round(numValM / 60, 1, MidpointRounding.ToEven);
            string finValM = Convert.ToString(numValM);
            string finValH = Convert.ToString(numValH);

            Window.textbox_avg.Text = numValM + " Minutes";
        }

        public void topgame(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT TOP 6 timetrack.gamelink AS GameName, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack GROUP BY timetrack.gamelink ORDER BY Sum(timetrack.sessiontime) DESC;";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            string t1 = "0";
            string t2 = "0";
            string t3 = "0";
            string t4 = "0";
            string t5 = "0";
            try
            {
                Window.label_top1.Content = dtResults.Rows[0]["GameName"].ToString();
                Window.label_top11.Content = dtResults.Rows[0]["SumOfsessiontime"].ToString() + " Minutes";
                t1 = dtResults.Rows[0]["SumOfsessiontime"].ToString();
                Window.label_top2.Content = dtResults.Rows[1]["GameName"].ToString();
                Window.label_top22.Content = dtResults.Rows[1]["SumOfsessiontime"].ToString() + " Minutes";
                t2 = dtResults.Rows[1]["SumOfsessiontime"].ToString();
                Window.label_top3.Content = dtResults.Rows[2]["GameName"].ToString();
                Window.label_top33.Content = dtResults.Rows[2]["SumOfsessiontime"].ToString() + " Minutes";
                t3 = dtResults.Rows[2]["SumOfsessiontime"].ToString();
                Window.label_top4.Content = dtResults.Rows[3]["GameName"].ToString();
                Window.label_top44.Content = dtResults.Rows[3]["SumOfsessiontime"].ToString() + " Minutes";
                t4 = dtResults.Rows[3]["SumOfsessiontime"].ToString();
                Window.label_top5.Content = dtResults.Rows[4]["GameName"].ToString();
                Window.label_top55.Content = dtResults.Rows[4]["SumOfsessiontime"].ToString() + " Minutes";
                t5 = dtResults.Rows[4]["SumOfsessiontime"].ToString();
            }
            catch (Exception ex)
            { }

            int t1c = Convert.ToInt32(t1);
            int t2c = Convert.ToInt32(t2);
            int t3c = Convert.ToInt32(t3);
            int t4c = Convert.ToInt32(t4);
            int t5c = Convert.ToInt32(t5);

            int tc = t1c + t2c + t3c + t4c + t5c;

            Window.textbox_tpmp.Text = Convert.ToString(tc) + " Minutes";

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1.Content + ".jpg"))
            {
                Window.image_cover1.Stretch = Stretch.Fill;
                Window.image_cover1.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2.Content + ".jpg"))
            {
                Window.image_cover2.Stretch = Stretch.Fill;
                Window.image_cover2.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3.Content + ".jpg"))
            {
                Window.image_cover3.Stretch = Stretch.Fill;
                Window.image_cover3.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4.Content + ".jpg"))
            {
                Window.image_cover4.Stretch = Stretch.Fill;
                Window.image_cover4.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5.Content + ".jpg"))
            {
                Window.image_cover5.Stretch = Stretch.Fill;
                Window.image_cover5.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5.Content + ".jpg"));
            }
        }

        public void topgame_day(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            int playday = DateTime.Today.Day;

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT TOP 6 timetrack.gamelink AS GameName, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack WHERE playday=" + playday + " GROUP BY timetrack.gamelink ORDER BY Sum(timetrack.sessiontime) DESC;";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            string t1 = "0";
            string t2 = "0";
            string t3 = "0";
            string t4 = "0";
            string t5 = "0";
            try
            {
                Window.label_top1_1.Content = dtResults.Rows[0]["GameName"].ToString();
                Window.label_top11_1.Content = dtResults.Rows[0]["SumOfsessiontime"].ToString() + " Minutes";
                t1 = dtResults.Rows[0]["SumOfsessiontime"].ToString();
                Window.label_top2_1.Content = dtResults.Rows[1]["GameName"].ToString();
                Window.label_top22_1.Content = dtResults.Rows[1]["SumOfsessiontime"].ToString() + " Minutes";
                t2 = dtResults.Rows[1]["SumOfsessiontime"].ToString();
                Window.label_top3_1.Content = dtResults.Rows[2]["GameName"].ToString();
                Window.label_top33_1.Content = dtResults.Rows[2]["SumOfsessiontime"].ToString() + " Minutes";
                t3 = dtResults.Rows[2]["SumOfsessiontime"].ToString();
                Window.label_top4_1.Content = dtResults.Rows[3]["GameName"].ToString();
                Window.label_top44_1.Content = dtResults.Rows[3]["SumOfsessiontime"].ToString() + " Minutes";
                t4 = dtResults.Rows[3]["SumOfsessiontime"].ToString();
                Window.label_top5_1.Content = dtResults.Rows[4]["GameName"].ToString();
                Window.label_top55_1.Content = dtResults.Rows[4]["SumOfsessiontime"].ToString() + " Minutes";
                t5 = dtResults.Rows[4]["SumOfsessiontime"].ToString();
            }
            catch (Exception ex)
            { }

            int t1c = Convert.ToInt32(t1);
            int t2c = Convert.ToInt32(t2);
            int t3c = Convert.ToInt32(t3);
            int t4c = Convert.ToInt32(t4);
            int t5c = Convert.ToInt32(t5);

            int tc = t1c + t2c + t3c + t4c + t5c;

            Window.textbox_tpt.Text = Convert.ToString(tc) + " Minutes";

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1_1.Content + ".jpg"))
            {
                Window.image_cover1_1.Stretch = Stretch.Fill;
                Window.image_cover1_1.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1_1.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2_1.Content + ".jpg"))
            {
                Window.image_cover2_1.Stretch = Stretch.Fill;
                Window.image_cover2_1.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2_1.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3_1.Content + ".jpg"))
            {
                Window.image_cover3_1.Stretch = Stretch.Fill;
                Window.image_cover3_1.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3_1.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4_1.Content + ".jpg"))
            {
                Window.image_cover4_1.Stretch = Stretch.Fill;
                Window.image_cover4_1.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4_1.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5_1.Content + ".jpg"))
            {
                Window.image_cover5_1.Stretch = Stretch.Fill;
                Window.image_cover5_1.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5_1.Content + ".jpg"));
            }
        }

        public void topgame_week(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            int playweek = weekNum;

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT TOP 6 timetrack.gamelink AS GameName, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack WHERE playweek=" + playweek + " GROUP BY timetrack.gamelink ORDER BY Sum(timetrack.sessiontime) DESC;";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            string t1 = "0";
            string t2 = "0";
            string t3 = "0";
            string t4 = "0";
            string t5 = "0";
            try
            {
                Window.label_top1_11.Content = dtResults.Rows[0]["GameName"].ToString();
                Window.label_top11_11.Content = dtResults.Rows[0]["SumOfsessiontime"].ToString() + " Minutes";
                t1 = dtResults.Rows[0]["SumOfsessiontime"].ToString();
                Window.label_top2_11.Content = dtResults.Rows[1]["GameName"].ToString();
                Window.label_top22_11.Content = dtResults.Rows[1]["SumOfsessiontime"].ToString() + " Minutes";
                t2 = dtResults.Rows[1]["SumOfsessiontime"].ToString();
                Window.label_top3_11.Content = dtResults.Rows[2]["GameName"].ToString();
                Window.label_top33_11.Content = dtResults.Rows[2]["SumOfsessiontime"].ToString() + " Minutes";
                t3 = dtResults.Rows[2]["SumOfsessiontime"].ToString();
                Window.label_top4_11.Content = dtResults.Rows[3]["GameName"].ToString();
                Window.label_top44_11.Content = dtResults.Rows[3]["SumOfsessiontime"].ToString() + " Minutes";
                t4 = dtResults.Rows[3]["SumOfsessiontime"].ToString();
                Window.label_top5_11.Content = dtResults.Rows[4]["GameName"].ToString();
                Window.label_top55_11.Content = dtResults.Rows[4]["SumOfsessiontime"].ToString() + " Minutes";
                t5 = dtResults.Rows[4]["SumOfsessiontime"].ToString();
            }
            catch (Exception ex)
            { }

            int t1c = Convert.ToInt32(t1);
            int t2c = Convert.ToInt32(t2);
            int t3c = Convert.ToInt32(t3);
            int t4c = Convert.ToInt32(t4);
            int t5c = Convert.ToInt32(t5);

            int tc = t1c + t2c + t3c + t4c + t5c;

            Window.textbox_tpw.Text = Convert.ToString(tc) + " Minutes";

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1_11.Content + ".jpg"))
            {
                Window.image_cover1_11.Stretch = Stretch.Fill;
                Window.image_cover1_11.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1_11.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2_11.Content + ".jpg"))
            {
                Window.image_cover2_11.Stretch = Stretch.Fill;
                Window.image_cover2_11.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2_11.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3_11.Content + ".jpg"))
            {
                Window.image_cover3_11.Stretch = Stretch.Fill;
                Window.image_cover3_11.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3_11.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4_11.Content + ".jpg"))
            {
                Window.image_cover4_11.Stretch = Stretch.Fill;
                Window.image_cover4_11.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4_11.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5_11.Content + ".jpg"))
            {
                Window.image_cover5_11.Stretch = Stretch.Fill;
                Window.image_cover5_11.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5_11.Content + ".jpg"));
            }
        }

        public void topgame_month(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            int playmonth = DateTime.Today.Month;

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT TOP 6 timetrack.gamelink AS GameName, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack WHERE playmonth=" + playmonth + " GROUP BY timetrack.gamelink ORDER BY Sum(timetrack.sessiontime) DESC;";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            string t1 = "0";
            string t2 = "0";
            string t3 = "0";
            string t4 = "0";
            string t5 = "0";
            try
            {
                Window.label_top1_12.Content = dtResults.Rows[0]["GameName"].ToString();
                Window.label_top11_12.Content = dtResults.Rows[0]["SumOfsessiontime"].ToString() + " Minutes";
                t1 = dtResults.Rows[0]["SumOfsessiontime"].ToString();
                Window.label_top2_12.Content = dtResults.Rows[1]["GameName"].ToString();
                Window.label_top22_12.Content = dtResults.Rows[1]["SumOfsessiontime"].ToString() + " Minutes";
                t2 = dtResults.Rows[1]["SumOfsessiontime"].ToString();
                Window.label_top3_12.Content = dtResults.Rows[2]["GameName"].ToString();
                Window.label_top33_12.Content = dtResults.Rows[2]["SumOfsessiontime"].ToString() + " Minutes";
                t2 = dtResults.Rows[2]["SumOfsessiontime"].ToString();
                Window.label_top4_12.Content = dtResults.Rows[3]["GameName"].ToString();
                Window.label_top44_12.Content = dtResults.Rows[3]["SumOfsessiontime"].ToString() + " Minutes";
                t4 = dtResults.Rows[3]["SumOfsessiontime"].ToString();
                Window.label_top5_12.Content = dtResults.Rows[4]["GameName"].ToString();
                Window.label_top55_12.Content = dtResults.Rows[4]["SumOfsessiontime"].ToString() + " Minutes";
                t5 = dtResults.Rows[4]["SumOfsessiontime"].ToString();
            }
            catch (Exception ex)
            { }

            int t1c = Convert.ToInt32(t1);
            int t2c = Convert.ToInt32(t2);
            int t3c = Convert.ToInt32(t3);
            int t4c = Convert.ToInt32(t4);
            int t5c = Convert.ToInt32(t5);

            int tc = t1c + t2c + t3c + t4c + t5c;

            Window.textbox_tpm.Text = Convert.ToString(tc) + " Minutes";

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1_12.Content + ".jpg"))
            {
                Window.image_cover1_12.Stretch = Stretch.Fill;
                Window.image_cover1_12.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top1_12.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2_12.Content + ".jpg"))
            {
                Window.image_cover2_12.Stretch = Stretch.Fill;
                Window.image_cover2_12.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top2_12.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3_12.Content + ".jpg"))
            {
                Window.image_cover3_12.Stretch = Stretch.Fill;
                Window.image_cover3_12.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top3_12.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4_12.Content + ".jpg"))
            {
                Window.image_cover4_12.Stretch = Stretch.Fill;
                Window.image_cover4_12.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top4_12.Content + ".jpg"));
            }
            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5_12.Content + ".jpg"))
            {
                Window.image_cover5_12.Stretch = Stretch.Fill;
                Window.image_cover5_12.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + @"\Gamecover\" + Window.label_top5_12.Content + ".jpg"));
            }
        }

        public void gamescomplecal(string gamename)
        {
            string sql = "SELECT * FROM gamecentral WHERE gamename = '" + gamename + "'";
            gamesresult(sql);

            if (sqlresult16 == "Prior to WGM" || sqlresult16 == "0")
            {
                sql = @"UPDATE gamecentral SET completed='Yes', completedresult='1' WHERE gamename = '" + gamename + "'";
                gamesupdate(sql);
            }

            if (sqlresult16 == "1")
            {
                int completednumber = Convert.ToInt32(sqlresult16);
                completednumber++;
                string completedsnumber = Convert.ToString(completednumber);

                sql = @"UPDATE gamecentral SET completed='Yes', completedresult='" + completedsnumber + "' WHERE gamename = '" + gamename + "'";
                gamesupdate(sql);
            }
        }

        public void total_game(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT Count(gamecentral.gamename) AS CountOfgamename FROM gamecentral";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            sqlresultt4 = dtResults.Rows[0]["CountOfgamename"].ToString();
            Window.textbox_gamelibrary.Text = sqlresultt4 + " Games";
        }

        public void total_gamecompleted(PageStats Window)
        {
            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "SELECT Count(gamecentral.gamename) AS CountOfgamename FROM gamecentral WHERE completed='Yes' OR gamecompatibility='Broken'";

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            sqlresultt5 = dtResults.Rows[0]["CountOfgamename"].ToString();
            int CountOfgamenameCompleted = Convert.ToInt32(sqlresultt5);
            int CountOfgamename = Convert.ToInt32(sqlresultt4);

            decimal percent = (CountOfgamenameCompleted * 100) / CountOfgamename;

            Window.textbox_gamecompleted.Text = sqlresultt5 + " Games / " + percent + "% of the Library";
        }

        public void Action(PageStats Window, int mode)
        {
            Window.listview_action.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Action' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Action' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_action.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_action.SelectedIndex = 0;
        }

        public void Aventure(PageStats Window, int mode)
        {
            Window.listview_aventure.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Aventure' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Aventure' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_aventure.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_aventure.SelectedIndex = 0;
        }

        public void FPS(PageStats Window, int mode)
        {
            Window.listview_fps.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='FPS' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='FPS' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_fps.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_fps.SelectedIndex = 0;
        }

        public void MMORPG(PageStats Window, int mode)
        {
            Window.listview_mmorpg.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='MMORPG' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='MMORPG' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_mmorpg.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_mmorpg.SelectedIndex = 0;
        }

        public void Racing(PageStats Window, int mode)
        {
            Window.listview_racing.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Racing' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Racing' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_racing.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_racing.SelectedIndex = 0;
        }

        public void RPG(PageStats Window, int mode)
        {
            Window.listview_rpg.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='RPG' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='RPG' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_rpg.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_rpg.SelectedIndex = 0;
        }

        public void Simulation(PageStats Window, int mode)
        {
            Window.listview_simulation.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Simulation' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Simulation' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_simulation.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_simulation.SelectedIndex = 0;
        }

        public void Strategy(PageStats Window, int mode)
        {
            Window.listview_strategy.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Strategy' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Strategy' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_strategy.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_strategy.SelectedIndex = 0;
        }

        public void Survival(PageStats Window, int mode)
        {
            Window.listview_survival.Items.Clear();

            string sql = "";
            if (mode == 1)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Survival' ORDER BY gamename";
            }
            if (mode == 2)
            {
                sql = "SELECT * FROM gamecentral WHERE gametype='Survival' ORDER BY lastplayed";
            }

            OleDbCommand cmd = new OleDbCommand(sql, conn); conn.Open(); OleDbDataReader reader; reader = cmd.ExecuteReader();

            try
            {
                while (reader.Read())
                {
                    Window.listview_survival.Items.Add(new { Col1 = reader.GetString(1).ToString(), Col2 = reader.GetString(12).ToString() });
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }
            finally
            {
                reader.Close(); conn.Close();
            }

            Window.listview_survival.SelectedIndex = 0;
        }

        public void topgame_playtime(PageStats Window, int mode)
        {
            Window.listview_tplaytime.Items.Clear();

            DataTable dtResults = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                if (mode == 1)
                {
                    dbCommand.CommandText = "SELECT timetrack.gamelink AS gamelink, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack GROUP BY timetrack.gamelink ORDER BY timetrack.gamelink";
                }
                if (mode == 2)
                {
                    dbCommand.CommandText = "SELECT timetrack.gamelink, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack GROUP BY timetrack.gamelink ORDER BY Sum(timetrack.sessiontime) DESC";
                }

                OleDbDataReader dbReader = dbCommand.ExecuteReader();

                if (dbReader.HasRows)
                    dtResults.Load(dbReader);

                dbReader.Close();
                conn.Close();
            }

            DataTable dtResults2 = new DataTable();
            conn.Open();

            if (conn.State == ConnectionState.Open)
            {
                OleDbCommand dbCommand = conn.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                if (mode == 1)
                {
                    dbCommand.CommandText = "SELECT timetrack.gamelink, Sum(timetrack.sessiontime) AS SumOfsessiontime FROM timetrack GROUP BY timetrack.gamelink ORDER BY Sum(timetrack.sessiontime) DESC";

                    OleDbDataReader dbReader2 = dbCommand.ExecuteReader();

                    if (dbReader2.HasRows)
                        dtResults2.Load(dbReader2);

                    dbReader2.Close();
                }
                conn.Close();
            }

            int rows = 0;
            int rows1 = 0;
            int posit = 1;
            int loop = 0;
            try
            {
                while (rows != null)
                {
                    if (mode == 1)
                    {
                        while (loop == 0)
                        {
                            if (dtResults.Rows[rows]["gamelink"].ToString() == dtResults2.Rows[rows1]["gamelink"].ToString())
                            {
                                Window.listview_tplaytime.Items.Add(new { Col0 = posit, Col1 = dtResults.Rows[rows]["gamelink"].ToString(), Col2 = dtResults.Rows[rows]["SumOfsessiontime"].ToString() });
                                rows++;
                                loop = 1;
                            }
                            rows1++;
                            posit++;
                        }
                        rows1 = 0;
                        posit = 1;
                        loop = 0;
                    }
                    if (mode == 2)
                    {
                        Window.listview_tplaytime.Items.Add(new { Col0 = posit, Col1 = dtResults.Rows[rows]["gamelink"].ToString(), Col2 = dtResults.Rows[rows]["SumOfsessiontime"].ToString() });
                        rows++;
                        posit++;
                    }
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); return; }

            Window.listview_tplaytime.SelectedIndex = 0;
        }
    }
}
